package com.dindang.ddm.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.dindang.ddm.mvp.contract.ContactContract;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.longxi.gamer_base.model.LoginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


@FragmentScope
public class ContactPresenter extends BasePresenter<ContactContract.Model, ContactContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private boolean isFirst = true;

    @Inject
    public ContactPresenter(ContactContract.Model model, ContactContract.View rootView) {
        super(model, rootView);
    }

    public void initDate(){
        if(!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        if(isFirst){
            LogUtils.debugInfo("Contact页面重新加载");
            isFirst = false;
            ArmsUtils.makeText(mApplication, "联系人数据加载");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccess(LoginEvent loginEvent){
        if(!isFirst) {
            isFirst = true;
            initDate();
        }
    }
}
